<?php

class FormElementGroup
{
    /**
     * @var string
     */
    private $name;

    /**
     *
     * @var array
     */
    protected $elements = array();

    /**
     *
     * @param array $elements
     */
    public function  __construct($name, array $elements = array()) {

        $this->name = $name;
        
        foreach ($elements as $element) {
            $this->setElement($element);
        }
    }

    /**
     *
     * @return string
     */
    public function __toString()
    {
        return $this->toString();
    }

    /**
     *
     * @param string $name
     * @return AbstractFormElement|FormElementGroup
     */
    public function getElement($name)
    {
        return isset($this->elements[$name]) ? $this->elements[$name] : null;
    }

    /**
     *
     * @return array
     */
    public function getElements()
    {
        return $this->elements;
    }

    /**
     * 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Returns validation messages of all elements.
     *
     * @return array
     */
    public function getValidationMessages()
    {
        $messages = array();
        foreach ($this->elements as $element) {
            if ($element->getValidationMessages() !== null) {
                $messages = array_merge($messages, $element->getValidationMessages());
            }
        }
        return $messages;
    }

    /**
     *
     * @return array
     */
    public function getValues()
    {
        $values = array();
        foreach ($this->elements as $name => $element) {
            $values[$name] = $element->getValue();
        }
        return $values;
    }

    /**
     * Enter description here...
     *
     * @return boolean
     */
    public function isValid()
    {
        $valid = true;
        foreach ($this->elements as $element) {
            if (!$element->isValid()) {
                $valid = false;
            }
        }
        return $valid;
    }

    /**
     *
     * @param AbstractFormElement $element
     * @return FormElementGroup
     */
    public function setElement(AbstractFormElement $element)
    {
        $this->elements[$element->getName()] = $element;
        $element->setName($this->name . '[' . $element->getName() . ']');
        return $this;
    }

    /**
     *
     * @param IFormElementDecorator $decorator
     * @return FormElementGroup 
     */
    public function setDecorator(IFormDecorator $decorator)
    {
        $this->decorator = $decorator;
        foreach ($this->elements as $element) {
            $element->setDecorator($decorator);
        }
        return $this;
    }

    /**
     *
     * @param array $values
     * @return FormElementGroup 
     */
    public function setValues(array $values)
    {
        foreach ($this->elements as $name => $element) {
            if (array_key_exists($name, $values)) {
                $element->setValue($values[$name]);
            }
        }
        return $this;
    }

    /**
     *
     * @return string
     */
    public function toString()
    {
        if ($this->decorator == null) {
            return $this->name;
        } else {
            return $this->decorator->decorateFormElementGroup($this);
        }
    }
}